package org.hrqing.chess.frame;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.hrqing.chess.config.props.SystemConfigProps;
import org.hrqing.chess.util.ImageUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.swing.*;
import java.awt.*;

/**
 * @author Hrqing
 */
@Slf4j
public class CenterBackgroundPanel extends JPanel {

    @Getter
    @Setter
    @NoArgsConstructor
    @AllArgsConstructor
    public static class ExecuteTime {
        private long start = System.currentTimeMillis();
        private long red = 0L;
        private long black = 0L;
        private long differ = 0L;
    }

    public static final String[][] COLUMN_NAME = {
            new String[]{"1", "九"},
            new String[]{"2", "八"},
            new String[]{"3", "七"},
            new String[]{"4", "六"},
            new String[]{"5", "五"},
            new String[]{"6", "四"},
            new String[]{"7", "三"},
            new String[]{"8", "二"},
            new String[]{"9", "一"},
    };

    public static final int[] COLUMN_WIDTH = {
            7,
            77,
            77,
            77,
            76,
            76,
            77,
            77,
            77,
            77,
    };

    public static final int[] ROW_HEIGHT = {
            10,
            79,
            79,
            80,
            79,
            81,
            79,
            79,
            79,
            79,
            79,
    };

    private final Image image;
    @Getter
    @Setter
    private String gameId;
    @Getter
    @Setter
    private String name;
    @Setter
    @Getter
    private ChessLabel.Color color;
    @Setter
    @Getter
    private ChessLabel.Color executing;
    @Getter
    @Setter
    private ExecuteTime executeTime;
    @Getter
    @Setter
    private SystemConfigProps.Depth depth;

    public CenterBackgroundPanel() {
        this.image = ImageUtils.loadImage("image/plate.png");
        GridBagLayout gridBagLayout = new GridBagLayout();
        gridBagLayout.columnWidths = COLUMN_WIDTH;
        gridBagLayout.rowHeights = ROW_HEIGHT;
        gridBagLayout.columnWeights = new double[]{1.0};  //设置了列的宽度为容器宽度
        gridBagLayout.rowWeights = new double[]{0.2, 0.8};  //第一行的高度占了容器的2份，第二行的高度占了容器的8份
        this.setLayout(gridBagLayout);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        // 544  618
        Graphics2D g2d = (Graphics2D) g;

        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BICUBIC);
        g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        int width = getWidth();
        int height = getHeight();

        // 自适应缩放策略
        int imgWidth = image.getWidth(null);
        int imgHeight = image.getHeight(null);

        double scaleX = (double) width / imgWidth;
        double scaleY = (double) height / imgHeight;
        double scale = Math.min(scaleX, scaleY);

        int drawWidth = (int) (imgWidth * scale);
        int drawHeight = (int) (imgHeight * scale);
        int drawX = (width - drawWidth) / 2;
        int drawY = (height - drawHeight) / 2;

        // 高质量绘制
        g2d.drawImage(image,
                drawX, drawY, drawX + drawWidth, drawY + drawHeight,
                0, 0, imgWidth, imgHeight,
                null);

            /*if (getLayout() instanceof GridBagLayout gbl) {
                try {
                    // 提取行高数据
                    int[] rowHeights = gbl.rowHeights;
                    int[] columnWidths = gbl.columnWidths;

                    // 绘制水平网格线（动态行高）
                    int y = 0;
                    for (int rowHeight : rowHeights) {
                        y += rowHeight;
                        g2d.setColor(new Color(248, 31, 100, 173));
                        g2d.drawLine(0, y, width, y);
                    }

                    // 绘制垂直网格线
                    int x = 0;
                    for (int colWidth : columnWidths) {
                        x += colWidth;
                        g2d.setColor(new Color(12, 12, 226, 231));
                        g2d.drawLine(x, 0, x, height);
                    }
                } catch (Exception ex) {
                    log.error("Error while painting background", ex);
                }
            }*/

    }

    public static int getColumnIndex(int x) {
        int sum = 0;
        for (int i = 0; i < COLUMN_WIDTH.length; i++) {
            sum += COLUMN_WIDTH[i];
            if (x <= sum) {
                return i;
            }
        }
        return -1;
    }

    public static int getColumnSum(int x) {
        int sum = 0;
        for (int i = 0; i < COLUMN_WIDTH.length; i++) {
            sum += COLUMN_WIDTH[i];
            if (x <= (i + 1)) {
                return sum;
            }
        }
        return -1;
    }

    public static int getRowIndex(int y) {
        int sum = 0;
        for (int i = 0; i < ROW_HEIGHT.length; i++) {
            sum += ROW_HEIGHT[i];
            if (y <= sum) {
                return i;
            }
        }
        return -1;
    }

    public static int getRowSum(int y) {
        int sum = 0;
        for (int i = 0; i < ROW_HEIGHT.length; i++) {
            sum += ROW_HEIGHT[i];
            if (y <= (i + 1)) {
                return sum;
            }
        }
        return -1;
    }

    public void executing(ChessLabel.Color color) {
        this.executing = color;
        switch (color) {
            case RED -> {
                this.executeTime.setRed(System.currentTimeMillis());
            }
            case BLACK -> {
                this.executeTime.setBlack(System.currentTimeMillis());
            }
        }
    }
}
